<?php namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\Category;
//栏目控制器
class Cate extends AdminBase{
	private $db;
	
	public function _initialize(){
		parent::_initialize();
		$this->db=new Category();
	}
	//栏目列表
	public function index(){
		$data=$this->db->getAllData();
//		halt($data);
		$this->assign('data',$data);
		return $this->fetch();
	}
	//添加栏目
	public function add(){
		if(request()->isPost()){
			$res=$this->db->addData(input('post.'));
			if($res['valid']){
				$this->success($res['msg']);exit;
				
			}else{
				$this->error($res['msg']);exit;
			}
			
		}else{
			
			return $this->fetch();
		}
		
		
	}
	
	//添加子栏目
	public function addson(){
		if(request()->isPost()){
			$res=$this->db->addData(input('post.'));
//			if($res['valid']){
//				$this->success($res['msg']);exit;
//				
//			}else{
//				$this->error($res['msg']);exit;
//			}
			
		}else{
			$cid=input('cid');
			if($cid){
				
				$this->assign('cid',$cid);
			}
			$data=$this->db->getAllData();
			
			$this->assign('data',$data);
			return $this->fetch();
		}
		
	}
	//栏目编辑
	public function edit(){
		if(request()->isPost()){
		
			$res=$this->db->editData(input('post.'));
//			halt(input('post.'));
			if($res['valid']){
				
				$this->success($res['msg'],'index');exit;
				
			}else{
				
				$this->error($res['msg']);exit;
			}
		}else{
			
			$cid=input('cid');
			//获得对应的栏目数据
			$oneData=db('category')->where('cid',$cid)->find();
			$this->assign('oneData',$oneData);
//			halt($oneData);
//         调用找子类的方法
            $cids=$this->getSon(db('category')->select(),$cid);
//          halt($cids);
            $data=$this->db->getAllData();
//          halt($data);
            //父级下的自己不能选中
            foreach($data as $k=>$v){
            	if(in_array($v['cid'],$cids)){
            		$data[$k]['_html']="disabled='disabled' style='background:#DDDDDD'";
            	}else{
            		$data[$k]['_html']='';
            	}
            	
            }
//          halt($data);
            $this->assign('data',$data);
	    	return $this->fetch();
			
		}
	
	}
	//删除
	public function delete(){
		$cid=input('cid');
	    $cids=$this->getSon(db('category')->select(),$cid);
//	    echo count($cids);exit;
	    if(count($cids)==1){
	    	db('category')->delete($cid);
	    	$this->success('栏目删除成功！');
	    }else{
	    	$this->error('请先删除子类！');
	    }
	}
	
	//排序
	public function sort(){
		$sorts=input('post.');
//	   var_dump($sorts);
		foreach($sorts as $k=>$v){
			if(empty($v)||$v>100){
				$this->error('栏目排序只能在1-100之间');
			}else{
				db('category')
				    ->where('cid',$k)
				    ->setField('sort',$v);
			}
        }
	   $this->success('栏目排序成功！');
	}
	
	
	//递归寻找子类
	public function getSon($data,$cid){
		$arr=[];
		
		if(!is_array($data)){
			return false;
		}
		
		foreach($data as $k=>$v){
//			   
			if($v['pid']==$cid){
//				
//              把子类的cid压入数组
                $arr[]=$v['cid'];
//             var_dump($arr);
//            
				$arr=$this->getSon($data,$v['cid']);
			}
		}
		//把自己压人
		 $arr[]=$cid;
//		halt($arr);
		return $arr;
	}
}

